package preprocessing;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.regex.*;

/**
 * @author Thomas Wangler, wangler_thomas@hotmail.com
 * Translation Memories for patent translation
 * Filereader
 * Preprocessing class that splits the corpus into test, train and devset according to metainformation 
 * (year published)
 */
public class Filereader {
	
	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

		//files
		String metafile = "D:\\pattr\\pattr\\claims\\pattr.de-en.claims.meta";
		String enfile = "D:\\pattr\\pattr\\claims\\pattr.de-en.claims.en";
		String defile = "D:\\pattr\\pattr\\claims\\pattr.de-en.claims.de";
		
		String testfileEN = "D:\\pattr\\partitionedCorpus\\pattr.testset.en";
		String testfileDE = "D:\\pattr\\partitionedCorpus\\pattr.testset.de";
		String testfileMETA = "D:\\pattr\\partitionedCorpus\\pattr.testset.meta";
		String devfileEN = "D:\\pattr\\partitionedCorpus\\pattr.devset.en";
		String devfileDE = "D:\\pattr\\partitionedCorpus\\pattr.devset.de";
		String devfileMETA = "D:\\pattr\\partitionedCorpus\\pattr.devset.meta";
		String trainfileEN = "D:\\pattr\\partitionedCorpus\\pattr.trainset.en";
		String trainfileDE = "D:\\pattr\\partitionedCorpus\\pattr.trainset.de";
		String trainfileMETA = "D:\\pattr\\partitionedCorpus\\pattr.trainset.meta";
		
		//streams
		FileInputStream metaStream = null;
		FileInputStream enStream = null;
		FileInputStream deStream = null;
		
		FileOutputStream testENStream = null;
		FileOutputStream testDEStream = null;
		FileOutputStream testMETAStream = null;
		FileOutputStream devENStream = null;
		FileOutputStream devDEStream = null;
		FileOutputStream devMETAStream = null;
		FileOutputStream trainENStream = null;
		FileOutputStream trainDEStream = null;
		FileOutputStream trainMETAStream = null;

		
		
		try {
			
			testENStream = new FileOutputStream(testfileEN);
			testDEStream = new FileOutputStream(testfileDE);
			testMETAStream = new FileOutputStream(testfileMETA);
			devENStream = new FileOutputStream(devfileEN);
			devDEStream = new FileOutputStream(devfileDE);
			devMETAStream = new FileOutputStream(devfileMETA);
			trainENStream = new FileOutputStream(trainfileEN);
			trainDEStream = new FileOutputStream(trainfileDE);
			trainMETAStream = new FileOutputStream(trainfileMETA);
			
			metaStream = new FileInputStream(metafile);
			enStream = new FileInputStream(enfile);
			deStream = new FileInputStream(defile);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	    
		
		Writer testSetWriterEN = new BufferedWriter(new OutputStreamWriter(testENStream, "UTF8"));
		Writer testSetWriterDE = new BufferedWriter(new OutputStreamWriter(testDEStream, "UTF8"));
		Writer testSetWriterMETA = new BufferedWriter(new OutputStreamWriter(testMETAStream, "UTF8"));
		
		Writer devSetWriterEN = new BufferedWriter(new OutputStreamWriter(devENStream, "UTF8"));
		Writer devSetWriterDE = new BufferedWriter(new OutputStreamWriter(devDEStream, "UTF8"));
		Writer devSetWriterMETA = new BufferedWriter(new OutputStreamWriter(devMETAStream, "UTF8"));
		
		Writer trainSetWriterEN = new BufferedWriter(new OutputStreamWriter(trainENStream, "UTF8"));
		Writer trainSetWriterDE = new BufferedWriter(new OutputStreamWriter(trainDEStream, "UTF8"));
		Writer trainSetWriterMETA = new BufferedWriter(new OutputStreamWriter(trainMETAStream, "UTF8"));
		
		
		
		
		BufferedReader metareader = new BufferedReader(new InputStreamReader(metaStream, "UTF8"));
	    BufferedReader enreader = new BufferedReader(new InputStreamReader(enStream, "UTF8"));
	    BufferedReader dereader = new BufferedReader(new InputStreamReader(deStream, "UTF8"));
	
	    String metaline = metareader.readLine();
	    String enline = enreader.readLine();
	    String deline = dereader.readLine();
        
	    Pattern yearpattern = Pattern.compile(".+\t.+\t([0-9]{4}?).+");
	    
	    while(metaline != null && enline != null && enline != null){

	    	Matcher yearmatch = yearpattern.matcher(metaline);
	    	if(yearmatch.matches()){
	    		int year = Integer.parseInt(yearmatch.group(1));
	    	
	    		switch(year){
            	case 2000:
            		testSetWriterEN.write(enline + "\n");
            		testSetWriterDE.write(deline + "\n");
            		testSetWriterMETA.write(metaline + "\n");
            		break;
            	case 1996: case 1998: case 2002:
            	case 2004:
            		devSetWriterEN.write(enline + "\n");
            		devSetWriterDE.write(deline + "\n");
            		devSetWriterMETA.write(metaline + "\n");
            		break;
            	default:
            		trainSetWriterEN.write(enline + "\n");
            		trainSetWriterDE.write(deline + "\n");
            		trainSetWriterMETA.write(metaline + "\n"); 	
	    		}
	    	}
            //advancing Readers
	    	metaline = metareader.readLine();
            enline = enreader.readLine();
            deline = dereader.readLine();

        }
	    
	    testENStream.close();
		testDEStream.close();
		testMETAStream.close();
		devENStream.close();
		devDEStream.close();
		devMETAStream.close();
		trainENStream.close();
		trainDEStream.close();
		trainMETAStream.close();
		
		metaStream.close();
		enStream.close();
		deStream.close();
	    
	    
	}

}
